package com.bm.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bm.entity.GcAl;
import com.bm.entity.News;
import com.bm.entity.PageBean;
import com.bm.service.NewsService;
import com.bm.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class InsertXwzxServlet
 */
@WebServlet("/InsertXwzxServlet")
public class InsertXwzxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertXwzxServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		NewsService ns = new NewsServiceImpl();
		String pd = request.getParameter("xw");
		String pd1 = request.getParameter("type");
		
		
		if(pd1.equals("delete")) {
			News news = new News();
			int id = Integer.parseInt(request.getParameter("id")); 
			news.setNewsId(id);
			boolean bl = ns.sdeleteXw(news);
			if(bl == true) {
				PageBean<News> xwList = new PageBean<News>();
				xwList = ns.pageList(xwList);
				request.setAttribute("xwList",xwList);
				request.getRequestDispatcher("selectGc.jsp").forward(request, response);
			}else {
				
				request.getRequestDispatcher("selectGc.jsp").forward(request, response);
			}
		}
		if(pd.equals("xw")) {
			News news = new News();
			String urls = request.getParameter("urls"); //图片路径
			String xwm = request.getParameter("xwm"); //名
			String texts = request.getParameter("texts"); //文本
			String neirong = request.getParameter("neirong"); //内容
			if(urls.equals("")) {
				String xx = "添加失败图片路径为空";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}else if(xwm.equals("")) {
				String xx = "添加失败名为空";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}else if(texts.equals("")) {
				String xx = "添加失败文本为空";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}else if(texts.equals("")) {
				String xx = "添加失败内容为空";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}
			news.setNewsImage(urls);
			news.setNewsName(xwm);
			news.setNewsTitle(texts);
			news.setNewsContent(neirong);
			boolean tf = ns.sinsertXw(news);
			if(tf == true) {
				String xx = "添加成功,请在新闻咨询查看";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}else {
				String xx = "添加失败";
				request.setAttribute("xx", xx);
				request.getRequestDispatcher("insert.jsp").forward(request, response);
			}
			
			
			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
